package main

import (
	"flag"
	"go-monitor/server/g"
	gohttp "go-monitor/server/http"
	gorpc "go-monitor/server/rpc"
	"go-monitor/server/service"
	"os"

	uservice "github.com/kardianos/service"
)

func main() {
	cfg := &uservice.Config{
		Name:        "go-server",
		DisplayName: "go-monitor server service",
		Description: "this is an go-monitor server service",
	}
	prg := &service.Program{Run: rund}
	ser, err := uservice.New(prg, cfg)
	if err != nil {
		g.Logger.Error("生成服务异常", err)
	}
	if len(os.Args) > 1 {
		err = uservice.Control(ser, os.Args[1])
		if err != nil {
			g.Logger.Error("生成服务异常1", err)
		}
	} else {
		ser.Run()
	}
}

func rund() {
	g.Logger.Info("server start")

	cfg := flag.String("c", "cfg.json", "configuration file")
	flag.Parse()
	g.ParseConfig(*cfg)

	service.InitRedis()

	service.InitAuth()

	go gorpc.StartRPC()

	go gohttp.StartHTTP()

	select {}
}
